package cn.tjuscs.selenium;




import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstSelenium {
	public static void main(String[] args) throws IOException {
		// 一、declaration and instantiation of objects/variables
		// 构造 XSSFWorkbook 对象，strPath 传入文件路径  
		XSSFWorkbook xwb = new XSSFWorkbook("C:\\Users\\lenovo\\Desktop\\Selenium+Lab2020.xlsx");  
		// 读取第一章表格内容  
		XSSFSheet sheet = xwb.getSheetAt(0);  
		// 定义 row、cell  
		for(int i=0;i<20;i++) {
			XSSFRow row=sheet.getRow(i);
			XSSFCell cella=row.getCell(1);
			XSSFCell cellb=row.getCell(2);
			String cellStringa = cella.getStringCellValue();  
			String cellStringb = cellb.getStringCellValue();
			
			//1. 定义FireFox浏览器驱动
			String driverPath = System.getProperty("user.dir") + "/src/resources/driver/geckodriver.exe";
			System.setProperty("webdriver.gecko.driver", driverPath);
			System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
			WebDriver driver = new FirefoxDriver();
				
			//2. 定义Chrome浏览器驱动
//			String driverPath = System.getProperty("user.dir") + "/src/resources/driver/chromedriver.exe";
//			System.setProperty("webdriver.chrome.driver", driverPath);
//			WebDriver driver = new ChromeDriver();
			
			//comment the above 2 lines and uncomment below 2 lines to use Chrome
			//System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
			//WebDriver driver = new ChromeDriver();
			
//			driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
//			driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
//			driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
	    	

	        //二、定义要访问的网址
	        //String baseUrl = "http://www.tju.edu.cn";
			String baseUrl = "http://103.120.226.190/selenium-demo/git-repo";
	        String expectedTitle = cellStringb;
	        String actualTitle = "";

	        //三、访问给定的网址launch Fire fox and direct it to the Base URL
	        driver.get(baseUrl);

	        //四、得到网址的title， get the actual value of the title
	        WebElement userName = driver.findElement(By.name("user_number"));
			userName.sendKeys(cellStringa);
			WebElement passName = driver.findElement(By.name("password"));
			passName.sendKeys(cellStringb);
			WebElement submitName = driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/div[2]/div/form/div[3]/input"));
			submitName.click();
			WebElement textShow = driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/div[2]/div/form/div[5]/code"));
	        //actualTitle = driver.getTitle();
	        

	        /*
	         * 五、比对Title和预期值compare the actual title of the page with the expected one and print
	         * the result as "Passed" or "Failed"
	         */
	        //if (actualTitle.contentEquals(expectedTitle)){
	        //    System.out.println("Test Passed!");
	        //} else {
	        //    System.out.println("Test Failed");
	        //}
			//assertThat(textShow, is(expectedTitle));
			actualTitle = textShow.getText();
			if (actualTitle.contentEquals(expectedTitle)){
	            System.out.println("Test Passed!");
	        } else {
	            System.out.println("Test Failed");
	        }
			System.out.println(cellStringa);
			System.out.println(cellStringb);
	        
	       
	        //close Fire fox
	        //driver.close();
		}
		
	}

}
